<template>
  <div v-loading="isloading" class="prossececontainer">
    <div class="selectHeader">
      <div class="select-wrapper">
        <div class="selectBox">
          <div class="selectTitle">
            <i class="iconfont icon-sousuo" />按条件查询
          </div>
          <el-form
            ref="form"
            :model="formv"
            label-width="150px"
            label-position="left"
          >
            <el-form-item label="编制单位名称:">
              <el-input
                v-model="listquery.key"
                clearable
                @change="valueChange"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <div>
      <div class="header">
        <div class="header-title">
          <i class="el-icon-d-arrow-right"></i>编制单位列表
        </div>

        <div class="uploadbutton" style="display:flex;align-items:center">
            <el-upload
            class="upload-demo"
            action=""
            :http-request="inputBlackList"
            :show-file-list="false"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          >
            <el-button type="danger" icon="el-icon-link" size="small">导入黑名单</el-button>
          </el-upload>
          <el-button size="small" type="warning" @click="open" style="margin-left:15px">
            <i class="el-icon-plus"></i>新增黑名单</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        stripe
        border
        style="width: 100%"
        class="eltable"
      >
        <el-table-column label="序号" width="150" type="index" />
        <el-table-column prop="profileName" label="编制单位名称" />
        <el-table-column prop="contactPhone" label="联系电话" />
        <el-table-column prop="username" label="用户名 " width="200" />

        <el-table-column label="操作 " width="400" fixed="right">
          <template scope="scope">
            <el-button
              type="danger"
              @click="handleDelete(scope.row.id)"
              size="mini"
              plain
              ><i class="el-icon-delete"></i>移出</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="tableData.length > 0"
        class="elpagi"
        :total="total"
        @size-change="getdata"
        @current-change="getdata"
        @prev-click="getdata"
        @next-click="getdata"
        :current-page.sync="listquery.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="listquery.pageSize"
        layout="total,sizes, prev, pager, next, jumper"
      />
    </div>
    <el-dialog
      title="新增黑名单"
      :visible.sync="isDialogShow"
      width="600px"
      :append-to-body="true"
      class="dialog1"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        label-position="left"
      >
        <el-form-item label="请选择要拉黑的编制单位:" prop="profileName">
          <el-select
            v-model="form.profileNameIds"
            placeholder="请选择"
            value-key="value"
            size="mini"
            multiple
            collapse-tags
            filterable
          >
            <el-option
              v-for="(item, index) in unittype"
              :key="item.name"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { profilelistSimple } from "@/api/index.js";
import { handleCofirm } from "@/utils/confirm";
import { getBlackList, addBlack, delBlack,addtos } from "@/api/shenhe.js";
import queryMixin from "@/mixins/countquery.js";
import XLSX from "xlsx";

export default {
  mixins: [queryMixin],
  data() {
    return {
      listquery: {
        pageSize: 10,
        currentPage: 1,
        key: "",
      },
      formv: {},
      //新增编辑弹窗
      isDialogShow: false,
      dialogStatus: "",
      rules: {
        profileNameIds: [
          { required: true, message: "请选择编制单位", trigger: "blur" },
        ],
      },
      form: {
        profileNameIds: [],
      },
      iswraploading: false,
      allvilages: [],
      unittype: [],
      isloading: false,
      changeDialogVisible: false,
      cityChildren: [],
      tableData: [],
    };
  },
  mounted() {
    this.getdata();
    this.getunittype();
  },
  methods: {
    open() {
      this.form = {
        profileNameIds: [],
      };
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.isDialogShow = true;
    },
      inputBlackList(data) {
      let file = data.file;
      let name = file.name;
      let fileType = name.split(".")[name.split(".").length - 1];
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message.error("仅支持Excel文件");
        return;
      }
      this.isloading = true;
      var reader = new FileReader();
      reader.onload = (e) => {
        var data = e.target.result;
        var workbook = XLSX.read(data, { type: "binary" });
        if (workbook) {
          let worksheet1 = workbook.Sheets[workbook.SheetNames[0]];
          let sheet = XLSX.utils.sheet_to_json(worksheet1, { header: 1 });
          let sheetData = [];
          for (let i = 1; i < sheet.length; i++) {
            const element = sheet[i];
            if (!element[0] || element[0] == null || element[0].trim() == "") {
            } else {
              if (element.length < 1) {
                this.$message.error("表格不能存在空值");
              }
              sheetData.push({
                 profileName: element[0].toString().trim(),
             
              });
            }
          }
          addtos(sheetData)
            .then((res) => {
              if (res.success) {
                this.$notify({
                  title: "导入成功",
                  message: res.message,
                  type: "success",
                  duration: 2000,
                });
                this.isloading = false;
                this.getdata();
              } else {
                this.$notify.error({
                  title: "导入失败",
                  message: res.message,
                });
                this.isloading = false;
              }
            })
            .catch((e) => {
              this.$message.error(e.response.data.message);
              this.isloading = false;
            });
        }
      };
      reader.readAsBinaryString(file);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const temp = Object.assign({}, this.form);
          addBlack(temp)
            .then((v) => {
              this.getdata();
              this.$notify({
                title: "成功",
                message: "添加成功",
                type: "success",
              });
              this.isDialogShow = false;
            })
            .catch((error) => {
              this.$notify.error({
                title: "错误",
                message: error,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    handleDelete(id) {
      handleCofirm("确认要将该单位移除黑名单吗？", "warning").then((res) => {
        delBlack(id)
          .then((v) => {
            this.getdata();
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
            });
            this.isDialogShow = false;
          })
          .catch((error) => {
            this.$notify.error({
              title: "错误",
              message: error,
            });
          });
      });
    },

    async getdata() {
      this.isloading = true;
      let res = await getBlackList(this.listquery);
      this.tableData = res.data.content;
      this.total = res.data.totalElements;
      this.isloading = false;
    },
    async getunittype() {
      let data = {
        pageSize: 500,
        currentPage: 1,
      };
      let res = await profilelistSimple(data);
      this.unittype = [];
      res.data.content.forEach((element) => {
        this.unittype.push({
          name: element.name,
          id: element.id,
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../../public/css/table.scss";
@import "../../../../public/css/countheader.scss";
.spandiv {
  margin-right: 5px;
}
::v-deep .el-select .el-input {
  width: 265px;
}
.prossececontainer {
  margin: 5px;
  padding-bottom: 10px;
  overflow-y: auto;
  position: relative;
  height: 100%;
}
ul {
  margin: 0;
  padding: 5px;
  list-style: none;
  border: 1px dashed #ccc;
  max-height: 250px;
  overflow-y: auto;
  li {
    line-height: 25px;
  }
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  line-height: 45px;
  width: 100%;
  .header-title {
    font-size: 15px;
    font-weight: 600;
    color: #4778c7;
  }
}
.allvilagestitle {
  line-height: 35px;
  color: #4778c7;
}

.more {
  color: #4cc8c7;
  margin-left: 10px;
  &:hover {
    color: orange;
  }
}
</style>
